﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace exsportance.Silverlight.Common.Helper
{
    /// <summary>
    /// A composite transition
    /// </summary>
    public class CompositeTransition : TransitionBase
    {
        #region members
        private TransitionBase _transitionOne;
        private TransitionBase[] _transitions;
        private Int32 _count;
        private UserControl _newPage;
        private UserControl _oldPage;
        #endregion

        #region constructor
        public CompositeTransition(TransitionBase transitionOne, params TransitionBase[] transitions)
        {
            _transitionOne = transitionOne;
            _transitions = transitions;
        }
        #endregion

        #region public methods
        public override void PerformTranstition(UserControl newPage, UserControl oldPage)
        {

            _newPage = newPage;

            _oldPage = oldPage;

            _transitionOne.TransitionCompleted += transition_TransitionCompleted;

            _transitionOne.PerformTranstition(newPage, oldPage);

            foreach (TransitionBase transition in _transitions)
            {
                transition.TransitionCompleted += transition_TransitionCompleted;
                transition.PerformTranstition(newPage, oldPage);
            }

        }

        void transition_TransitionCompleted(object sender, TransitionCompletedEventArgs e)
        {
            _count++;

            if (_count == _transitions.Length + 1)
                OnTransitionCompleted(_newPage, _oldPage);
        }
        #endregion


    }
}
